package cn.com.dhcc.ddi.stat.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.BaseService;
import cn.com.dhcc.app.core.util.UUIDGenerator;
import cn.com.dhcc.app.pub.core.util.dateformatter.DateFormatter;
import cn.com.dhcc.ddi.stat.dao.DataSynDao;
import cn.com.dhcc.ddi.stat.vo.DataSyncStatVo;

/**
 * 按节点统计服务类
 * @日期：2017/01/16 16:50:24
 * @作者：李博宇
 * @版权所有：东华软件
 * @版本：1.0
 */
@Service
public class DataSyncStatService extends BaseService{
	@Autowired
	private DataSynDao dao;
	/**
	 * 
	    * @Title: dailyDataStatistics
	    * @Description: 按节点每日统计
	    * @param @throws ServiceException    参数
	    * @return void    返回类型
	    * @throws
	 */
	public void dailyDataStatistics() throws ServiceException {
		try {
			List<DataSyncStatVo> sends = dao.getDailyNodeSendStatistics();
			List<DataSyncStatVo> recvs = dao.getDailyNodeRecvStatistics();
			
			for(DataSyncStatVo vo: sends) {
				vo.setID(UUIDGenerator.getUUID());
				vo.setMtime(new Date());
				vo.setStatDate(DateFormatter.getDateBasedOnTodayForDate("yy/MM/dd", -1));
				dao.insert(vo);
			}
			
			for(DataSyncStatVo vo: recvs) {
				vo.setID(UUIDGenerator.getUUID());
				vo.setMtime(new Date());
				vo.setStatDate(DateFormatter.getDateBasedOnTodayForDate("yy/MM/dd", -1));
				dao.insert(vo);
			}
		} catch (DaoException e) {
			throw new ServiceException("根据节点获取统计数据失败", e);
		}

		
	}
	
	public List<DataSyncStatVo> getDataStatSend(int scope, String date, String cataId,String condition) throws ServiceException {
		try {
			String start = "";
			String end = "";
			if(scope == 0) {
				start = date + " 00:00:00";
				end = date + " 23:59:59";
			} else if(scope == 1) {
				start = date + "-01 00:00:00";
				end = date + "-31 23:59:59";
			} else {
				start = date + "-01-01 00:00:00";
				end = date + "-12-31 23:59:59";
			}
			
			return dao.getDataStatData(start, end, condition, cataId, "0");
		} catch (DaoException e) {
			throw new ServiceException("根据节点获取统计数据失败", e);
		}
	}
	
	public List<DataSyncStatVo> getDataStatRecv(int scope, String date, String cataId,String condition) throws ServiceException {
		try {
			String start = "";
			String end = "";
			if(scope == 0) {
				start = date + " 00:00:00";
				end = date + " 23:59:59";
			} else if(scope == 1) {
				start = date + "-01 00:00:00";
				end = date + "-31 23:59:59";
			} else {
				start = date + "-01-01 00:00:00";
				end = date + "-12-31 23:59:59";
			}
			
			return dao.getDataStatData(start, end, condition, cataId, "1");
		} catch (DaoException e) {
			throw new ServiceException("根据节点获取统计数据失败", e);
		}
	}
	
	public PagerData<DataSyncStatVo> getDataStatPagerData(Pager pager, String date, String cataId, WhereCondition condition) throws ServiceException {
		try {	
			String start = date + " 00:00:00";
			String end = date + " 23:59:59";
			return dao.getDataStatPagerData(pager, start, end, cataId, condition);
		} catch (DaoException e) {
			throw new ServiceException("根据任务获取日历分页数据失败", e);
		}
	}
	
	public List<String> getDistinctNodes(WhereCondition nodeParameter,int scope, String date) throws ServiceException {
		try {
			String start = "";
			String end = "";
			if(scope == 0) {
				start = date + " 00:00:00";
				end = date + " 23:59:59";
			} else if(scope == 1) {
				start = date + "-01 00:00:00";
				end = date + "-31 23:59:59";
			} else {
				start = date + "-01-01 00:00:00";
				end = date + "-12-31 23:59:59";
			}
			List<DataSyncStatVo> list = dao.getDistinctNodes(nodeParameter,start, end);
			List<String> nodeNames = new ArrayList<String>();
			for(DataSyncStatVo vo: list) {
				nodeNames.add(vo.getNodeName());
			}
			return nodeNames;
		} catch (DaoException e) {
			throw new ServiceException("根据节点获取统计数据失败", e);
		}
	}
	
	public List<String> getDistinctCates(WhereCondition nodeParameter,int scope, String date) throws ServiceException {
		try {
			String start = "";
			String end = "";
			if(scope == 0) {
				start = date + " 00:00:00";
				end = date + " 23:59:59";
			} else if(scope == 1) {
				start = date + "-01 00:00:00";
				end = date + "-31 23:59:59";
			} else {
				start = date + "-01-01 00:00:00";
				end = date + "-12-31 23:59:59";
			}
			List<DataSyncStatVo> list = dao.getDistinctCates(nodeParameter,start, end);
			List<String> cataNames = new ArrayList<String>();
			for(DataSyncStatVo vo: list) {
				cataNames.add(vo.getDatacatagoryName());
			}
			return cataNames;
		} catch (DaoException e) {
			throw new ServiceException("根据节点获取统计数据失败", e);
		}
	}
}
